<?php


namespace App\Http\Controllers\Admin\Connect;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//人脉奖池道具
class PrizePooPropController extends BaseController
{
    /**
     * @api {post} /connect/prize-pool/prop 添加人脉奖池道具
     *
     * @apiGroup 人脉-奖池道具
     *
     * @apiParam {Int} prize_pool_id 奖池id，必须
     * @apiParam {Int} prize_pool_type 奖池礼物类型：1：本期礼物；2：下期礼物，必须
     * @apiParam {Int} prop_type 道具类型：1：礼物；2：商品，必须
     * @apiParam {Int} prop_id 道具id，必须
     * @apiParam {Int} prop_num 道具数量，必须
     * @apiParam {Int} use_days 使用天数，prop_type=2时必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('prize_pool_id', 'prize_pool_type', 'prop_type', 'prop_id', 'prop_num', 'use_days');
        $v = app('validator')->make($param, [
            'prize_pool_id' => 'required|integer',
            'prize_pool_type' => 'required|integer|in:1,2',
            'prop_type' => 'required|integer|in:1,2',
            'prop_id' => 'required|integer|min:1',
            'prop_num' => 'required|integer|min:1',
            'use_days' => 'required_if:prop_type,2|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_connect_prize_pool')->addOrEditProp($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /connect/prize-pool/prop 编辑人脉奖池道具
     *
     * @apiGroup 人脉-奖池道具
     *
     * @apiParam {Int} id 奖池礼物id，必须
     * @apiParam {Int} prize_pool_id 奖池id，必须
     * @apiParam {Int} prize_pool_type 奖池礼物类型：1：本期礼物；2：下期礼物，必须
     * @apiParam {Int} prop_type 道具类型：1：礼物；2：商品，必须
     * @apiParam {Int} prop_id 道具id，必须
     * @apiParam {Int} prop_num 道具数量，必须
     * @apiParam {Int} use_days 使用天数，prop_type=2时必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'prize_pool_id', 'prize_pool_type', 'prop_type', 'prop_id', 'prop_num', 'use_days');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'prize_pool_id' => 'required|integer',
            'prize_pool_type' => 'required|integer|in:1,2',
            'prop_type' => 'required|integer|in:1,2',
            'prop_id' => 'required|integer|min:1',
            'prop_num' => 'required|integer|min:1',
            'use_days' => 'required_if:prop_type,2|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_connect_prize_pool')->addOrEditProp($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {put} /connect/prize-pool/prop/num 编辑人脉奖池道具的数量
     *
     * @apiGroup 人脉-奖池道具
     *
     * @apiParam {Int} id 奖池礼物id，必须
     * @apiParam {Int} prize_pool_type 奖池礼物类型：1：本期礼物；2：下期礼物，必须
     * @apiParam {Int} prop_change_num 变化数量，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function editNum(Request $request)
    {
        $param = $request->only('id', 'prize_pool_type', 'prop_change_num');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'prize_pool_type' => 'required|integer|in:1,2',
            'prop_change_num' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $id = $param['id'];
        $prizePoolType = $param['prize_pool_type'];
        $propChangeNum = $param['prop_change_num'];

        if ($propChangeNum == 0) {
            throw new BasicException(0, '无意义的数量变化');
        }

        DB::beginTransaction();
        try {
            switch ($prizePoolType) {
                case 1:
                    $prizePoolPropModel = app('model_connect_prize_pool_prop');
                    $prizePoolPropModel->where('id', $id)->increment('remaining_stock', $propChangeNum);
                    break;
                case 2:
                    $prizePoolPropModel = app('model_connect_prize_pool_prop_next');
                    break;
            }
            $prizePoolPropModel->where('id', $id)->increment('init_stock', $propChangeNum);

            $prizePoolProp = $prizePoolPropModel->select('id')->where('id', $id)->first();
            if (empty($prizePoolProp)) throw new BasicException(0, 'id错误，记录不存在');

            DB::commit();
            return Response::sendData([]);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(0, $e->getMessage());
        }
    }

    /**
     * @api {get} /connect/prize-pool/prop 获取人脉奖池道具
     *
     * @apiGroup 人脉-奖池道具
     *
     * @apiParam {Int} prize_pool_id 奖池id，必须
     * @apiParam {Int} prize_pool_type 奖池礼物类型：1：本期礼物；2：下期礼物，必须
     * @apiParam {Int} status 状态：1（默认）：上架；0：下架，仅本期奖池需要，且非必须传
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1, 序号
    "prop_type": 1,
    "prop_id": 87,
    "init_stock": 1000, 初始数量
    "remaining_stock": 1000, 剩余数量
    "prop_type_name": "礼物", 道具类型名称
    "prop_name": "情书" 道具名称
    }
    ]
    }
     */
    public function list(Request $request)
    {
        $param = $request->only('prize_pool_id', 'prize_pool_type', 'status');
        $v = app('validator')->make($param, [
            'prize_pool_id' => 'required|integer',
            'prize_pool_type' => 'required|integer|in:1,2',
            'status' => 'integer|in:0,1'
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $prizePoolId = $param['prize_pool_id'];
        $prizePoolType = $param['prize_pool_type'];
        $status = $param['status'] ?? 1;

        $fields = ['id', 'prop_type', 'prop_id', 'init_stock'];
        switch ($prizePoolType) {
            case 1:
                $prizePoolPropModel = app('model_connect_prize_pool_prop')->where('status', $status);
                $fields[] = 'remaining_stock';
                break;
            case 2:
                $prizePoolPropModel = app('model_connect_prize_pool_prop_next');
                break;
        }

        $list = $prizePoolPropModel->select($fields)
            ->where('prize_pool_id', $prizePoolId)
            ->get();

        foreach ($list as &$v) {

            //道具名称
            if ($v->prop_type == 1) {
                $v->prop_type_name = '礼物';
                $v->prop_name = app('model_gift')->where('id', $v->prop_id)->value('name');
            } else if ($v->prop_type == 2) {
                $v->prop_type_name = '商品';
                $v->prop_name = app('model_mall_products')->where('id', $v->prop_id)->value('title');
            } else {
                $v->prop_type_name = '';
                $v->prop_name = '';
            }
        }
        return Response::sendData($list);
    }

    /**
     * @api {delete} /connect/prize-pool/prop 删除人脉奖池道具
     *
     * @apiGroup 人脉-奖池道具
     *
     * @apiParam {Int} prize_pool_type 奖池礼物类型：1：本期礼物；2：下期礼物，必须
     * @apiParam {Int} id 要删除的id
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $param = $request->only('prize_pool_type', 'id');
        $v = app('validator')->make($param, [
            'prize_pool_type' => 'required|integer|in:1,2',
            'id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $prizePoolType = $param['prize_pool_type'];
        $id = $param['id'];

        switch ($prizePoolType) {
            case 1:
                $prizePoolPropModel = app('model_connect_prize_pool_prop');
                break;
            case 2:
                $prizePoolPropModel = app('model_connect_prize_pool_prop_next');
                break;
        }

        $prizePoolProp = $prizePoolPropModel->where('id', $id)->first();
        if (empty($prizePoolProp)) throw new BasicException(0, 'id错误，道具不存在');

        $res = $prizePoolProp->delete();
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '删除失败');
    }
}